//4.	Create a method that draws a Map with the signature:
//// draw Map.m_map w/ a nested for-loop in the console
//void draw()


//#define PRIMER4

#ifdef PRIMER4

#include <iostream>
#include <fstream>

#define MAX_LINE 256

class Map
{
private:
	char**	mMap;
	int		mHeight;
	int		mWidth;
	
public:
	Map() : mMap(0), mHeight(0), mWidth(0) {}
	~Map()
	{
		for (int i = 0; i < mHeight; ++i)
		{
			delete [] mMap[i];
		}
		delete [] mMap;
	}

	bool LoadFile(char* filename)
	{
		std::ifstream myFile;

		myFile.open(filename);

		if (!myFile.good())
			return false;

		char buffer[MAX_LINE];

		myFile.getline(buffer, MAX_LINE);

		bool heightFound = false;

		for(int i = 0; buffer[i] != '\n'; ++i)
		{
			int num = buffer[i] - '0'; // convert to an int in the right range

			if (num > 0 && num <= 9)
			{
				if (!heightFound)
				{
					// found the height
					mHeight = num;
					heightFound = true;
				}
				else
				{
					// found the width
					mWidth = num;
					break; // done!
				}
			}
		}

		// create the dynamically allocated array
		mMap = new char*[mHeight];
		for(int i = 0; i < mHeight; ++i)
		{
			mMap[i] = new char[mWidth];
		}

		// read the rest of the file
		int lineNum = 0;

		while(!myFile.eof())
		{
			myFile.getline(buffer, MAX_LINE); // gets a character from the file

			for(int i = 0; i < strlen(buffer); ++i)
			{
				mMap[lineNum][i] = buffer[i];
			}
			++lineNum;
		}
		return true;
	}

	// draws the map to the screen
	void Draw(void)
	{
		for(int i = 0; i < mHeight; i++)
		{
			for(int j = 0; j < mWidth; j++)
			{
				printf("%c", mMap[i][j]);
			}
			printf("\n");
		}
	}
};

int main()
{
	Map myMap;

	if (myMap.LoadFile("test4.txt"))
	{
		// do stuff
		myMap.Draw();
	}
	else
	{
		std::cout << "Error reading file!" << std::endl;
		return 0;
	}

	return 0;
}

#endif